//
//  YSMarkCtrl.m
//  Yueshi
//
//  Created by Northwan on 15/8/7.
//  Copyright (c) 2015年 悦食中国. All rights reserved.
//  收藏

#import "YSMarkCtrl.h"
#import "JXScrollBar.h"
#import "YSNiceProductCtrl.h"
#import "YSNiceArticleCtrl.h"
#import "YSVideoCollectionCtrl.h"

@interface YSMarkCtrl ()<UIScrollViewDelegate,JXScrollBarDelegate>

@property(nonatomic,strong)UIScrollView *scrollView;
@property(nonatomic,strong)NSMutableArray *childController;
@property(nonatomic,strong)NSMutableArray *titleArray;

@property(nonatomic,strong)JXScrollBar *scrollBar;

@end

@implementation YSMarkCtrl

-(BOOL)shouldLogin{
    return YES;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.fd_prefersNavigationBarHidden = NO;
    _childController = [NSMutableArray array];
    _titleArray = [NSMutableArray array];
    [_titleArray addObject:@"好货"];
    [_titleArray addObject:@"好文"];
    [_titleArray addObject:@"视频"];
    
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc]initWithTitle:@"编辑" style:UIBarButtonItemStylePlain target:self action:@selector(edit:)];
    rightItem.tintColor = HEXColor(@"#333333");
    self.navigationItem.rightBarButtonItem = rightItem;

    UIScrollView *scrollView = [[UIScrollView alloc]init];
    scrollView.frame = CGRectMake(0, 0, SCREENW, SCREENH);
    scrollView.showsHorizontalScrollIndicator = NO;
    scrollView.pagingEnabled = YES;
    //设置scrollview的代理
    scrollView.delegate = self;
    scrollView.scrollsToTop = NO;
    _scrollView = scrollView;
    [self.view addSubview:scrollView];
    JXScrollBar *scrollBar = [[JXScrollBar alloc]initWithFrame:CGRectMake(-SCALEX(20), 0, SCREENW-16, 44) type:JXScrollBarSplitTypeCenter];
    scrollBar.delegate = self;
    _scrollBar = scrollBar;
    self.navigationItem.titleView = _scrollBar;
    
    [self inflateLayout];
    
    
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(subPageCallBack:) name:kSubPageMarkStatusChangeNortif object:nil];
}

-(void)subPageCallBack:(NSNotification *)nortif{
    NSNumber *obj = [nortif object];
    if(obj.integerValue == hideStatus){
        self.navigationItem.rightBarButtonItem = nil;
    }else{
        UIBarButtonItem *rightItem = [[UIBarButtonItem alloc]initWithTitle:@"取消" style:UIBarButtonItemStylePlain target:self action:@selector(edit:)];
        rightItem.tintColor = HEXColor(@"#333333");
        [self edit:rightItem];
        [rightItem setTitle:@"编辑"];
        self.navigationItem.rightBarButtonItem = rightItem;
    }
}

-(void)edit:(UIBarButtonItem *)btnItem{
    NSString *title = btnItem.title;
    if([title isEqualToString:@"编辑"]){
        [self.navigationItem.rightBarButtonItem setTitle:@"取消"];
        [self.navigationItem.rightBarButtonItem setTintColor:HEXColor(@"#b9b9b9")];
        [[NSNotificationCenter defaultCenter]postNotificationName:kMarkStatusChangeNortif object:[NSNumber numberWithInt:editStatusType]];
    }else{
        [self.navigationItem.rightBarButtonItem setTitle:@"编辑"];
        [self.navigationItem.rightBarButtonItem setTintColor:HEXColor(@"#333333")];
        [[NSNotificationCenter defaultCenter]postNotificationName:kMarkStatusChangeNortif object:[NSNumber numberWithInt:noEditStatusType]];
    }
}

-(void)inflateLayout{
    for (UIViewController *ctrl in _childController) {
        [ctrl.view removeFromSuperview];
    }
    [_childController removeAllObjects];
    //    [_titleArray removeAllObjects];
    
    //添加好货页面
    YSNiceProductCtrl *niceProductCtrl = [[YSNiceProductCtrl alloc]init];
    [_childController addObject:niceProductCtrl];
    UIView *niceProductView = niceProductCtrl.view;
    niceProductView.frame =CGRectMake(0, 0, SCREENW, SCREENH-NavWithStatusBarH);
    [_scrollView addSubview:niceProductView];
    //添加好文页面
    YSNiceArticleCtrl *niceArticleCtrl = [[YSNiceArticleCtrl alloc]init];
    [_childController addObject:niceArticleCtrl];
    UIView *niceArticleView = niceArticleCtrl.view;
    niceArticleView.frame =CGRectMake(SCREENW, 0, SCREENW, SCREENH-NavWithStatusBarH);
    [_scrollView addSubview:niceArticleView];
    //添加视频界面
    YSVideoCollectionCtrl *videoCtrl = [YSVideoCollectionCtrl new];
    [_childController addObject:videoCtrl];
    UIView *videoCtrlView = videoCtrl.view;
    videoCtrlView.frame = CGRectMake(SCREENW*2, 0, SCREENW, SCREENH - NavWithStatusBarH);
    [_scrollView addSubview:videoCtrlView];
    
    //设置scrollBar的数据
    [_scrollBar setTitleArray:_titleArray];
    [_scrollBar scrollViewDidScroll:_scrollView];
    //使第1页去加载数据
    [self scrollViewDidEndDecelerating:_scrollView];
    _scrollView.contentSize = CGSizeMake(_titleArray.count*SCREENW, 0);
    
    
}
#pragma mark -jxscrollbar代理，处理点击title的button事件
- (void)titleClicked:(JXScrollBar *)scrollbar index:(int)index{
    [scrollbar autoScrollToCenter:index];
    [self setScrollCurrentPageNum:index];
}

#pragma mark -滚动代理
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    [_scrollBar scrollViewDidScroll:scrollView];
}

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    //当前页数
    int pageNum = (scrollView.contentOffset.x+scrollView.width*0.5)/scrollView.width;
    
    [self setScrollCurrentPageNum:pageNum];
    [_scrollBar scrollViewDidEndDecelerating:scrollView];
}

-(void)setScrollCurrentPageNum:(int)pageNum{
    CGPoint p = CGPointMake(pageNum*_scrollView.frame.size.width, 0);
    [_scrollView setContentOffset:p];
    //这句代码的作用是解决点击顶部之后底部的scrollview往上跑的问题
    _scrollView.y = 0;
    if ([_childController[pageNum] isKindOfClass:[YSNiceProductCtrl class]]) {
        YSNiceProductCtrl *controller = _childController[pageNum];
        [controller checkRequestData];
        //调用请求数据的方法
    }else if([_childController[pageNum] isKindOfClass:[YSNiceArticleCtrl class]]){
        YSNiceArticleCtrl *controller = _childController[pageNum];
        [controller checkRequestData];
    }else if ([_childController[pageNum] isKindOfClass:[YSVideoCollectionCtrl class]]){
        YSVideoCollectionCtrl *controller = _childController[pageNum];
        [controller checkRequestData];
    }
}

@end
